/*
 * txdnet.cn tonydon
 * by netbeans ide 7.2
 */
package cn.txdnet.txcms.dao;

import cn.txdnet.txcms.domain.Catalogs;
import cn.txdnet.txcms.domain.Infobase;
import cn.txdnet.txcms.dto.InfoDTO;
import cn.txdnet.txcms.dto.PageDTO;
import cn.txdnet.txcms.dto.TagsDTO;
import cn.txdnet.txcms.mapper.CatalogsMapper;
import cn.txdnet.txcms.mapper.InfobaseMapper;
import cn.txdnet.txcms.mapper.InfotagsMapper;
import cn.txdnet.txcms.query.InfobaseQuery;
import cn.txdnet.txcms.query.ViewInfoQuery;
import cn.txdnet.txcms.service.CatalogsLevelService;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.CollectionUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 信息资讯dao
 *
 * @author tangxiaodong
 */
public class InfobaseDAO extends MybatisBaseDAO {

    /**
     * 插入信息到数据库
     *
     * @param info
     * @return
     */
    public int insertInfo(Infobase info) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.insertInfobase(info);
    }

    /**
     * 删除信息
     *
     * @param ida
     * @return
     */
    public int deleteInfo(Map m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.deleteInfobase(m);
    }

    /**
     * 更新发布状态
     *
     * @param m
     * @return
     */
    public int updatePubstate(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updatePubstate(m);
    }

    /**
     * 更新评论状态
     *
     * @param m
     * @return
     */
    public int updateCmtstate(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateCmtstate(m);
    }

    /**
     * 批量更新信息标识
     *
     * @param m
     * @return
     */
    public int updateTFlag(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateTFlag(m);
    }

    /**
     * 更新标题颜色
     *
     * @param m
     * @return
     */
    public int updateTColor(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateTColor(m);
    }

    /**
     * 更新发布时间
     *
     * @param m
     * @return
     */
    public int updatePubTime(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updatePubTime(m);
    }

    /**
     * 更新截止发布时间
     *
     * @param m
     * @return
     */
    public int updateEndTime(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateEndTime(m);
    }

    /**
     * 更新信息权重
     *
     * @param m
     * @return
     */
    public int updateWeight(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateWeight(m);
    }

    /**
     * 更新栏目和类目
     *
     * @param m
     * @return
     */
    public int updateChannelAndCatalog(Map<String, Object> m) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateChannelAndCatalog(m);
    }

    /**
     * 更新信息
     *
     * @param info
     * @return
     */
    public int updateInfo(Infobase info) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.updateInfobase(info);
    }

    /**
     * 累加查看次数
     *
     * @param infoid
     * @return
     */
    public int increaseHitNum(long infoid) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.increaseHitNum(infoid);
    }

    /**
     * 得到当前信息最大权重值
     *
     * @return
     */
    public int queryMaxWeight() {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.queryMaxWeight();
    }

    /**
     * 查询记录
     *
     * @param query
     * @return
     */
    public PageDTO queryByFactor(InfobaseQuery query) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        query.filter();
        List list = mapper.listByFactor(query);
        if (query.getRownum().intValue() == 0 && CollectionUtil.isNotEmpty(list)) {
            query.setRownum(mapper.countByFactor(query));
        }
        return new PageDTO(list, query.getRownum());
    }

    /**
     * 查询信息用于管理员编辑界面展示
     *
     * @param infoid
     * @return
     */
    public Infobase queryInfobaseForEdit(long infoid) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.queryInfobaseForEdit(infoid);
    }

    /**
     * 查询信息记录 for view
     *
     * @param ViewInfoQuery
     * @return
     */
    public Infobase queryInfobaseForView(ViewInfoQuery query) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        return mapper.queryInfobaseForView(query);
    }

    /**
     * 前台类别信息列表用于页面板块显示
     *
     * @param viewInfoQuery
     * @return
     */
    public List<Infobase> listByFactorForView(ViewInfoQuery query) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        if (query.getCid() != null) {
            CatalogsMapper catalogsMapper = this.getMapper(CatalogsMapper.class);
            Catalogs ct = catalogsMapper.queryCatalogsByCid(query.getCid());
            int cataloglevel = CatalogsLevelService.getLevelByCatalog(ct);
            query.setCataloglevel(cataloglevel);
        }
        List<Infobase> list = mapper.listByFactorForView(query);
        if (CollectionUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }

    /**
     * 前台info list列表显示
     *
     * @param query
     * @return
     * @throws Exception
     */
    public List<InfoDTO> listInfoForView(ViewInfoQuery query) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        if (query.getCid() != null) {
            CatalogsMapper catalogsMapper = this.getMapper(CatalogsMapper.class);
            Catalogs ct = catalogsMapper.queryCatalogsByCid(query.getCid());
            int cataloglevel = CatalogsLevelService.getLevelByCatalog(ct);
            query.setCataloglevel(cataloglevel);
        }
        List<Infobase> list = mapper.listByFactorForView(query);
        if (CollectionUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<InfoDTO> infoDtoList = new ArrayList<InfoDTO>(list.size() + 1);
        List<Long> infoIdList = null;
        try {
            infoIdList = injectDtoListByEntity(infoDtoList, list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        InfotagsMapper infotagsMapper = this.getMapper(InfotagsMapper.class);
        List<TagsDTO> tagsDtoList = infotagsMapper.queryInfoTagsForView(infoIdList);
        if (CollectionUtil.isEmpty(tagsDtoList)) {
            return infoDtoList;
        }
        injectTagsDtoList(infoDtoList, tagsDtoList);
        return infoDtoList;
    }

    /**
     * 将infobase字段设置倒infoDto
     *
     * @param infoDtoList
     * @param infobaseList
     * @return
     * @throws Exception
     */
    private static List<Long> injectDtoListByEntity(List<InfoDTO> infoDtoList, List<Infobase> infobaseList) throws Exception {
        List<Long> infoIdList = new ArrayList<Long>(infobaseList.size() + 1);
        for (Infobase info : infobaseList) {
            InfoDTO dto = new InfoDTO();
            BeanUtil.copyProperties(dto, info);
            infoDtoList.add(dto);
            infoIdList.add(info.getInfoid());
        }
        return infoIdList;
    }

    /**
     * 将tagDto设置到对应的infoDto中
     *
     * @param infoDtoList
     * @param tagsDtoList
     */
    private static void injectTagsDtoList(List<InfoDTO> infoDtoList, List<TagsDTO> tagsDtoList) {
        for (InfoDTO infoDto : infoDtoList) {
            List<TagsDTO> tempTagsDtoList = new ArrayList<TagsDTO>(7);
            for (TagsDTO tagDto : tagsDtoList) {
                if (tagDto.getInfoid().equals(infoDto.getInfoid())) {
                    tempTagsDtoList.add(tagDto);
                }
            }
            infoDto.setTags(tempTagsDtoList);
        }
    }

    /**
     * 前台tag信息列表显示
     * @param query
     * @return 
     */
    public List<?> listByTagsForView(ViewInfoQuery query) {
        InfobaseMapper mapper = this.getMapper(InfobaseMapper.class);
        List<?> list = mapper.listByTagsForView(query);
        if (CollectionUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }
}